package com.control;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.emp.database.LoginOperate;

/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public class   Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		HttpSession session= request.getSession();
		Cookie[] ck=request.getCookies();
		String username = "";
		String password = "";
		//遍历数组获取客户端Cookie中的内容
		for(int i=0;ck!=null&&i<ck.length;i++){
			if(ck[i].getName().equals("username")){
				username=ck[i].getValue();
			}
			if(ck[i].getName().equals("password")){
				password=ck[i].getValue();
			}
		}
		if(!username.equals("")&&!password.equals("")){
			LoginOperate lo = new LoginOperate();
			int flag = lo.checkLogin(username,password);
			if(flag==1){
				session.setAttribute("uname",username);
				request.getRequestDispatcher("manager/manager.jsp").forward(request, response);
			}else{
				
				request.setAttribute("message", "用户名或密码错误");
				/*Cookie c3 = new Cookie("username","");
				c3.setMaxAge(60);
				response.addCookie(c3);*/
				request.getRequestDispatcher("Login.jsp").forward(request, response);
			}
		}
		
		request.getRequestDispatcher("Login.jsp").forward(request, response);
	}
	
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
